package com.normalClasses.eye;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
 * 爬取肺炎的逻辑，主要逻辑上在于解析json，在未来的某个时间，接口可能会停止更新，保守估计接口的使用时间应该在2020.10.18
 * @author lrc 
 *@since 2020.4.11
 */
public class Feiyan {
private final String url="http://view.inews.qq.com/g2/getOnsInfo?name=disease_h5";
private final String other="http://view.inews.qq.com/g2/getOnsInfo?name=disease_other";
public LinkedList<HashMap<String,String>> get_info(){
	String path="";
	String method = "GET";
	//参数需要，不含内容
	Map<String, String> headers = new HashMap<String, String>();
	//参数需要，不含内容
	Map<String, String> querys = new HashMap<String, String>();
	//定义结果返回
	LinkedList<HashMap<String,String>> res=new LinkedList<HashMap<String, String>>();
    try {
    	HttpResponse response = HttpUtils.doGet(this.url,path, method, headers, querys);
    	//获取response的body直接对json进行拿值
    	JSONObject data=JSON.parseObject(EntityUtils.toString(response.getEntity())).getJSONObject("data");
    	HttpResponse response1 = HttpUtils.doGet(this.other,path, method, headers, querys);
    	//获取response的body直接对json进行拿值
        JSONObject other_msg=JSON.parseObject(EntityUtils.toString(response1.getEntity()));
        String lastUpdateTime=data.getString("lastUpdateTime");
        JSONArray citydata=((JSONObject)data.getJSONArray("areaTree").get(0)).getJSONArray("children");
        for(int i=0;i<citydata.size();i++){
        	String province=((JSONObject)citydata.get(i)).getString("name").toString();
        	JSONArray detail=((JSONObject)citydata.get(i)).getJSONArray("children");
        	String zero=other_msg.getJSONObject("data").getJSONObject("provinceCompare").getJSONObject(province).getString("zero");
        	for(int j=0;j<detail.size();j++) {
        		if(!((JSONObject)(detail.get(j))).getString("name").toString().equals("境外输入")){
        			HashMap<String,String> user=new HashMap();
        			//键值封装
        			user.put("UpdateTime",lastUpdateTime);
        			user.put("province",province);
        			user.put("name",((JSONObject)(detail.get(j))).getString("name").toString());
        			user.put("total_confirm",((JSONObject)(detail.get(j))).getJSONObject("total").getString("confirm").toString());
        			user.put("total_healRate",((JSONObject)(detail.get(j))).getJSONObject("total").getString("healRate").toString());
        			user.put("total_deadRate",((JSONObject)(detail.get(j))).getJSONObject("total").getString("deadRate").toString());
        			user.put("today_confirm",((JSONObject)(detail.get(j))).getJSONObject("today").getString("confirm").toString());
        			user.put("today_isUpdated",((JSONObject)(detail.get(j))).getJSONObject("today").getString("isUpdated").toString());
        			user.put("zero",zero);
        			res.add(user);
        		}
        	}
        		
        }
    }catch (Exception e) {
    	e.printStackTrace();
    }
    return res;
    }
//空参数构造器
public Feiyan() {
}
}
